var roleResource = require('../model/roleResource');
var userModel = require('../model/user');

exports.selectAll = function(req, res, connection){
	roleResource.selectRoleResource(connection, function(rows){
		res.send(rows);
	});
};

exports.addRoleResource = function(req, res, connection){
	var data = req.body;
	var roleId = data.role;
	var resource = data.resource;
	if(resource == undefined){
		resource = [];
	}
	roleResource.addRoleResource(connection, roleId, resource, function(rows){
		res.send(rows);
	});
};

exports.updateRoleResource = function(req, res, connection){
	var data = req.body;
	var roleId = data.role;
	var resource = data.resource;
	if(resource == undefined){
		resource = [];
	}
	roleResource.updateRoleResource(connection, roleId, resource, function(rows){
		res.send(rows);
	});
};

exports.getResByUser = function(req, res, connection) {
	var user = req.user;
	if(user != null && user != undefined) {
		userModel.findResource(connection, user.id, function(rows) {
			res.send(rows);
		});
	}else{
		res.send(null);
	}

};